<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.materialCode"
          clearable
          placeholder="物料编码"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.materialBriefCode"
          clearable
          placeholder="物料简码"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.materialName"
          clearable
          placeholder="物料名称"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.materialType"
          clearable
          filterable
          placeholder="物料类型"
          style="width: 185px"
          class="filter-item"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in dict.material_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.materialSource"
          clearable
          filterable
          placeholder="物料来源"
          style="width: 185px"
          class="filter-item"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in dict.material_source"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="630px"
      >
        <el-form
          ref="form"
          :inline="true"
          :model="form"
          :rules="rules"
          size="small"
          label-width="90px"
        >
          <el-form-item label="物料编码" prop="materialCode">
            <el-input v-model="form.materialCode" style="width: 190px"/>
          </el-form-item>
          <el-form-item label="物料简码" prop="materialBriefCode">
            <el-input v-model="form.materialBriefCode" style="width: 190px"/>
          </el-form-item>
          <el-form-item label="物料名称" prop="materialName">
            <el-input v-model="form.materialName" style="width: 190px"/>
          </el-form-item>
          <el-form-item label="物料类型" prop="materialType">
            <el-select
              v-model="form.materialType"
              filterable
              placeholder="请选择"
            >
              <el-option
                v-for="item in dict.material_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="物料规格" prop="materialSpecification">
            <el-input v-model="form.materialSpecification" style="width: 190px"/>
          </el-form-item>
          <el-form-item label="物料来源" prop="materialSource">
            <el-select
              v-model="form.materialSource"
              filterable
              placeholder="请选择"
            >
              <el-option
                v-for="item in dict.material_source"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="品牌" prop="materialBrand">
            <el-input v-model="form.materialBrand" style="width: 190px"/>
          </el-form-item>
          <el-form-item label="单位" prop="materialUnit">
            <el-input v-model="form.materialUnit" style="width: 190px"/>
          </el-form-item>
          <el-form-item label="供应商编号" prop="supplier">
            <el-input v-model="form.supplier" style="width: 190px"/>
          </el-form-item>
          <el-form-item label="供应商名称" prop="supplierName">
            <el-input v-model="form.supplierName" style="width: 190px"/>
          </el-form-item>
          <el-form-item label="描述" prop="remark">
            <el-input v-model="form.remark" :rows="3" type="textarea" style="width: 480px"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
            >确认</el-button
          >
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column min-width="150" prop="materialCode" label="物料编码" width="120px;" align="center"/>
        <el-table-column
          min-width="150"
          width="150px;" align="center"
          prop="materialBriefCode"
          label="物料简码"
        />
        <el-table-column min-width="150" prop="materialName" label="物料名称" width="150px;" align="center"/>
        <el-table-column min-width="80" prop="materialType" label="物料类型" width="100px;" align="center">
          <template slot-scope="scope">
            {{ dict.label.material_type[scope.row.materialType] }}
          </template>
        </el-table-column>
        <el-table-column
          min-width="80"
          prop="materialSpecification"
          label="物料规格"
          width="150px;" align="center"
        />
        <el-table-column min-width="80" prop="materialSource" label="物料来源" width="80px;" align="center">
          <template slot-scope="scope">
            {{ dict.label.material_source[scope.row.materialSource] }}
          </template>
        </el-table-column>
        <el-table-column min-width="80" prop="materialBrand" label="品牌" width="80px;" align="center"/>
        <el-table-column min-width="80" prop="materialUnit" label="单位"width="80px;" align="center" />
        <el-table-column min-width="100" prop="supplier" label="供应商编号" width="120px;" align="center"/>
        <el-table-column
          min-width="100"
          prop="supplierName"
          label="供应商名称"
        />
        <el-table-column prop="remark" label="描述" width="250px;" align="center"/>
        <el-table-column prop="createBy" label="创建人" width="100px;" align="center"/>
        <el-table-column prop="createTime" label="创建时间" width="150px;" align="center" />
        <el-table-column prop="updateBy" label="更新人"width="100px;" align="center" />
        <el-table-column prop="updateTime" label="更新时间" width="150px;" align="center" />
        <el-table-column
          v-if="
            checkPer(['admin', 'productMaterial:edit', 'productMaterial:del'])
          "
          label="操作"
          fixed="right"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudProductMaterial from "@/api/product/productMaterial";
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import rrOperation from "@crud/RR.operation";
import crudOperation from "@crud/CRUD.operation";
import udOperation from "@crud/UD.operation";
import pagination from "@crud/Pagination";

const defaultForm = {
  id: null,
  materialCode: null,
  materialBriefCode: null,
  materialName: null,
  materialType: null,
  materialSpecification: null,
  materialSource: null,
  materialBrand: null,
  materialUnit: null,
  supplier: null,
  supplierName: null,
  remark: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null,
};
export default {
  name: "ProductMaterial",
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ["material_type", "material_source"],
  cruds() {
    return CRUD({
      title: "物料管理",
      url: "api/productMaterial",
      idField: "id",
      sort: "id,desc",
      optShow: {
        add: true,
        edit: false,
        del: true,
        download: false,
        reset: true,
        import: false,
      },
      crudMethod: { ...crudProductMaterial },
    });
  },
  data() {
    return {
      permission: {
        add: ["admin", "productMaterial:add"],
        edit: ["admin", "productMaterial:edit"],
        del: ["admin", "productMaterial:del"],
      },
      rules: {
        materialCode: [
          { required: true, message: "物料编号不能为空", trigger: "blur" },
        ],
        materialBriefCode: [
          { required: true, message: "物料简码不能为空", trigger: "blur" },
        ],
        materialName: [
          { required: true, message: "物料名称不能为空", trigger: "blur" },
        ],
        materialType: [
          { required: true, message: "物料类型不能为空", trigger: "blur" },
        ],
        materialSpecification: [
          { required: true, message: "物料规格不能为空", trigger: "blur" },
        ],
        materialSource: [
          { required: true, message: "物料来源不能为空", trigger: "blur" },
        ],
        materialUnit: [
          { required: true, message: "单位不能为空", trigger: "blur" },
        ],
      },
      queryTypeOptions: [
        { key: "materialCode", display_name: "物料编码" },
        { key: "materialBriefCode", display_name: "物料简码" },
        { key: "materialName", display_name: "物料名称" },
        { key: "materialType", display_name: "物料类型" },
        { key: "materialSource", display_name: "物料来源" },
      ],
    };
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true;
    },
  },
};
</script>

<style scoped>
</style>
